package com.oversea.action.school;

import java.util.Date;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.oversea.action.BaseAction;
import com.oversea.annotation.Permission;
import com.oversea.models.Admissionfator;
import com.oversea.models.Major;
import com.oversea.models.Majortype;
import com.oversea.models.School;
import com.oversea.service.AdmissionfatorService;
import com.oversea.service.MajorService;
import com.oversea.service.MajortypeService;
import com.oversea.service.SchoolService;
import com.oversea.util.BeanUtils;
import com.oversea.util.page.Page;
import com.oversea.util.page.PageUtil;
import com.oversea.util.searchcontext.SearchContext;
import com.oversea.util.searchcontext.SearchOption;
import com.oversea.util.searchcontext.SearchOption.Option;

import static com.oversea.util.ParamUtil.*;

@ParentPackage("oversea-default")
@InterceptorRefs( { @InterceptorRef("defaultStack") })
@Namespace("/school")
@Permission(level = 3)
public class SchoolAction extends BaseAction {

	private static final long serialVersionUID = -8199478948374178740L;

	private SchoolService schoolService;

	private MajortypeService majortypeService;

	private MajorService majorService;

	private AdmissionfatorService admissionfatorService;

	private List<School> schoolList;

	private List<Majortype> majortypeList;

	private List<Major> majorList;

	private List<Admissionfator> admissionfatorList;

	private School school;

	private String majorIds;

	private Admissionfator admissionfator;

	private Page page;

	private int objId;

	private String schoolName; // 学校名
	private String state; // 所在州
	private String city; // 所在城市
	private int schoolType = -1; // 类型（私立:0，公立:1)
	private int academicTerms = -1; // 学制（学季:0，学期:1）

	@SuppressWarnings("unchecked")
	public Class getActionClass() {
		return School.class;
	}

	private SearchContext getListSearchContext() {
		SearchContext searchContext = new SearchContext();
		if (isNotEmpty(schoolName)) {
			searchContext.addOption(new SearchOption("schoolName", "%"
					+ schoolName + "%", SearchOption.Option.like));
		}
		if (isNotEmpty(state)) {
			searchContext.addOption(new SearchOption("state", state,
					SearchOption.Option.eqaul));
		}
		if (isNotEmpty(city)) {
			searchContext.addOption(new SearchOption("city", city,
					SearchOption.Option.eqaul));
		}
		if (schoolType >= 0) {
			searchContext.addOption(new SearchOption("schoolType", schoolType,
					SearchOption.Option.eqaul));
		}
		if (academicTerms >= 0) {
			searchContext.addOption(new SearchOption("academicTerms",
					academicTerms, SearchOption.Option.eqaul));
		}
		searchContext.setSearchClass(getActionClass());
		return searchContext;
	}

	@Action(value = "toadd", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/school/school_add.ftl") })
	public String add() {
		SearchContext searchContext = new SearchContext();
		searchContext.setSearchClass(Majortype.class);
		majortypeList = majortypeService.getObjectTotalList(searchContext);
		return SUCCESS;
	}

	@Action(value = "add", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String save() {
		school.setCreatedTime(new Date());
		school.setModifiedTime(new Date());
		Admissionfator admissionfator = new Admissionfator();
		admissionfator.setCreatedTime(new Date());
		admissionfator.setModifiedTime(new Date());
		admissionfatorService.save(admissionfator);
		school.setAdmissionfator(admissionfator);
		schoolService.save(school);
		return SUCCESS;
	}

	@Action(value = "toaddmajor", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/school/school_add_major.ftl") })
	public String addMajor() {
		school = schoolService.getObjById(objId);
		SearchContext searchContext = new SearchContext();
		searchContext.setSearchClass(Major.class);
		majorList = majorService.getObjectTotalList(searchContext);
		return SUCCESS;
	}

	@Action(value = "addmajor", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String saveMajor() {
		school = schoolService.getObjById(objId);
		if (isNotEmpty(majorIds)) {
			school.setMajors("," + majorIds.replace(", ", ",") + ",");
		}
		schoolService.update(school);
		return SUCCESS;
	}

	@Action(value = "toaddadmission", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/school/school_add_admission.ftl") })
	public String addAdmission() {
		school = schoolService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "addadmission", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String saveAdmission() {
		School tempSchool = schoolService.getObjById(objId);
		Admissionfator tempAdmission = admissionfatorService
				.getObjById(tempSchool.getAdmissionfator().getId());
		admissionfator.setCreatedTime(tempAdmission.getCreatedTime());
		admissionfator.setModifiedTime(new Date());
		BeanUtils.copyProperties(tempAdmission, admissionfator);
		admissionfatorService.update(tempAdmission);
		return SUCCESS;
	}

	@Action(value = "toedit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/school/school_edit.ftl") })
	public String edit() {
		SearchContext searchContext = new SearchContext();
		searchContext.setSearchClass(Majortype.class);
		majortypeList = majortypeService.getObjectTotalList(searchContext);
		SearchContext searchContext2 = new SearchContext();
		searchContext2.setSearchClass(Major.class);
		majorList = majorService.getObjectTotalList(searchContext);
		school = schoolService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String update() {
		School temp = schoolService.getObjById(school.getId());
		/*
		 * 通过struts2上传的表单并school对象并没有包含Admissionfator字段对象
		 * 所以需要重新设置,防止copyProperties时被覆盖为null
		 */
		school.setAdmissionfator(temp.getAdmissionfator());
		school.setCreatedTime(temp.getCreatedTime());
		school.setModifiedTime(new Date());
		BeanUtils.copyProperties(temp, school);
		schoolService.update(temp);
		school = temp;
		return SUCCESS;
	}

	@Action(value = "toview", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/school/school_view.ftl") })
	public String view() {
		school = schoolService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "toviewmajors", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/school/school_view_major.ftl") })
	public String viewMajors() {
		school = schoolService.getObjById(objId);
		if (!isNotEmpty(school.getMajors())) {
			return SUCCESS;
		}
		SearchContext searchContext = new SearchContext();
		searchContext.setSearchClass(Major.class);
		searchContext.addOption(new SearchOption("id", school.getMajors()
				.substring(1, school.getMajors().length() - 1), Option.in));
		majorList = majorService.getObjectTotalList(searchContext);
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/school/school_list.ftl") })
	public String list() {
		page = PageUtil.createPage(pageSize, currentPage, majorService
				.getObjectCount(getListSearchContext()).intValue());
		schoolList = schoolService.getObjectList(getListSearchContext(),
				pageSize, currentPage);
		return SUCCESS;
	}

	@Action(value = "del", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String del() {
		School temp = schoolService.getObjById(objId);
		schoolService.delete(temp);
		return SUCCESS;
	}

	public SchoolService getSchoolService() {
		return schoolService;
	}

	public void setSchoolService(SchoolService schoolService) {
		this.schoolService = schoolService;
	}

	public MajortypeService getMajortypeService() {
		return majortypeService;
	}

	public void setMajortypeService(MajortypeService majortypeService) {
		this.majortypeService = majortypeService;
	}

	public MajorService getMajorService() {
		return majorService;
	}

	public void setMajorService(MajorService majorService) {
		this.majorService = majorService;
	}

	public List<Majortype> getMajortypeList() {
		return majortypeList;
	}

	public void setMajortypeList(List<Majortype> majortypeList) {
		this.majortypeList = majortypeList;
	}

	public List<Major> getMajorList() {
		return majorList;
	}

	public void setMajorList(List<Major> majorList) {
		this.majorList = majorList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getObjId() {
		return objId;
	}

	public void setObjId(int objId) {
		this.objId = objId;
	}

	public String getSchoolName() {
		return schoolName;
	}

	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getSchoolType() {
		return schoolType;
	}

	public void setSchoolType(int schoolType) {
		this.schoolType = schoolType;
	}

	public int getAcademicTerms() {
		return academicTerms;
	}

	public void setAcademicTerms(int academicTerms) {
		this.academicTerms = academicTerms;
	}

	public School getSchool() {
		return school;
	}

	public void setSchool(School school) {
		this.school = school;
	}

	public String getMajorIds() {
		return majorIds;
	}

	public void setMajorIds(String majorIds) {
		this.majorIds = majorIds;
	}

	public List<School> getSchoolList() {
		return schoolList;
	}

	public void setSchoolList(List<School> schoolList) {
		this.schoolList = schoolList;
	}

	public AdmissionfatorService getAdmissionfatorService() {
		return admissionfatorService;
	}

	public void setAdmissionfatorService(
			AdmissionfatorService admissionfatorService) {
		this.admissionfatorService = admissionfatorService;
	}

	public List<Admissionfator> getAdmissionfatorList() {
		return admissionfatorList;
	}

	public void setAdmissionfatorList(List<Admissionfator> admissionfatorList) {
		this.admissionfatorList = admissionfatorList;
	}

	public Admissionfator getAdmissionfator() {
		return admissionfator;
	}

	public void setAdmissionfator(Admissionfator admissionfator) {
		this.admissionfator = admissionfator;
	}

}
